package net.zjitc.servlet.product;

import net.zjitc.domain.Product;
import net.zjitc.service.impl.ProductServiceImpl;
import net.zjitc.service.product.ProductService;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

/**
 * Created by hasee on 2017/10/23.
 */
@WebServlet(name = "UpdateProductServlet",urlPatterns = "/updateProductServlet")
public class UpdateProductServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //在doPost里要设置编码格式
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        if("".equals(request.getParameter("pname"))){
            request.getSession().setAttribute("addMsg","请填写商品名称");
            response.sendRedirect("/manger/product/addproduct.jsp");
        }else {
            Map map = request.getParameterMap();
            Product product = new Product();
            try {
                BeanUtils.populate(product,map);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
            ProductService service = new ProductServiceImpl();
            service.update(product);
            response.sendRedirect("/manger/productListByPageServlet");
        }
    }
}
